<template>
  <div>
    <el-button type="primary" @click="addTrademark">+添加</el-button>
    <el-table
      :data="trademarkList"
      style="width: 100%" border>
      <el-table-column
        prop="prop"
        align="center"
        type="index"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌管理"
        width="width">
      </el-table-column>
      <el-table-column
        prop="prop"
        label="品牌Logo"
        width="width">
        <template slot-scope="{row,$index}">
          <img :src="row.logoUrl" alt="" style="width: 80px;height: 80px; ">
        </template>
      </el-table-column>

      <el-table-column
        prop="prop"
        label="操作"
        width="width">
        <template slot-scope="{row,$index}">
          <HintButton type="warning" icon="el-icon-edit" size="mini" title="修改"
                      @click="updateTrademark(row)"></HintButton>
          <HintButton type="danger" icon="el-icon-delete" size="mini" title="删除"
                      @click="deleteTrademark(row)">
          </HintButton>
        </template>
      </el-table-column>
    </el-table>
    <!--分页器-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="getTrademarkList"
      :current-page="page"
      align="center"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      :pager-count="7"
      layout=" prev, pager, next, jumper, ->, sizes, slot,total"
      :total="total">
    </el-pagination>
    <!--打开对话框-->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="tmForm">
        <el-form-item label="品牌名称" label-width="100px">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="100px">
          <!--上传图片图片区域-->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Trademark",
  data() {
    return {
      trademarkList: [],
      page: 1,//当前页码
      limit: 3, //每页记录数
      total: 0,//因为有分页器所以在这里需要初始化一个总数
      dialogFormVisible: false,//默认false对话框是关闭的 当点击的添加或者修改的的时候打开
      imageUrl: '',//图片相关的从官网复制过来的
      //添加页面需要收集的数据
      tmForm: {
        logoUrl: "",
        tmName: ""
      },
    }
  },
  mounted() {
    this.getTrademarkList()
  },
  methods: {
    //图片相关的
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      //console.log(res,file)
      this.tmForm.logoUrl = res.data

    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    async getTrademarkList(pager = 1) {
      this.page = pager
      try {
        const result = await this.$API.trademark.getPageTrademark(this.page, this.limit)
        //console.log(result)
        this.trademarkList = result.data.records
        this.total = result.data.total
      } catch (error) {
      }
    },
    //当分页器的页码更改时的回调
    handleSizeChange(size) {
      this.limit = size
      this.getTrademarkList()
    },
    //当点击添加按钮 打开dialog对话框
    addTrademark() {
      this.dialogFormVisible = true//打开对话框
      //当再次点击添加按钮时需要清空tmForm的数据
      this.tmForm= {
        logoUrl: "",
          tmName: ""
      }
    },
    //修改品牌
    updateTrademark(row) {
      this.dialogFormVisible = true
      this.tmForm={...row}//用到浅拷贝 获取当前点击的这一行的row的信息 让它等于 this.tmForm 即可显示名字和图片
    },
    //点击添加页面的保存按钮 提交数据到列表中
    async save() {
      //1获取参数2整理参数
      let trademark = this.tmForm
      try {
        //3.发请求
        const result = await this.$API.trademark.addOrUpdateTrademark(trademark)
        if (result.code === 200 || result.code === 20000) {
          //4成功
          this.$message.success(trademark.id ? '修改品牌数据成功' : '添加品牌数据成功')
          this.dialogFormVisible = false //关闭对话框
          await this.getTrademarkList(trademark.id > 1 ? this.page : 1)
        } else {
          this.$message.error(trademark.id ? '修改品牌数据失败' : '添加品牌数据失败')
        }
      } catch (error) {
        //5 失败
        this.$message('保存品牌请求失败')
      }

    },
    //删除品牌
    // deleteTrademark(row) {
    //   this.$confirm()
    //   try {
    //     this.$API.trademark.delete(row.id)
    //     this.$message('删除品牌成功')
    //     this.getTrademarkList()
    //   } catch (error) {
    //     this.$message('删除品牌请求失败')
    //   }
    // }
    deleteTrademark(row) {
      this.$confirm(`你确定要删除${row.tmName}吗`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await this.$API.trademark.delete(row.id)
          this.$message.success('删除品牌成功')
          //删除成功后要看当前页面有几个 如果比1大 就停留在当前页,如果只有一个 就去上一页
          await this.getTrademarkList(this.trademarkList.length > 1 ? this.page : this.page - 1)
        } catch (error) {
          this.$message.error('删除品牌失败' + error.message)
        }
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
